//
//  UIView+Cool.m
//  HX2Car
//
//  Created by Mudmen on 16/7/31.
//  Copyright © 2016年 Hx2car_mac. All rights reserved.
//

#import "UIView+Cool.h"

@implementation UIView (Cool)

- (CGRect)getBoundsWithImage:(UIImage*)image{
    
    CGSize size = self.frame.size;
    CGSize toSize = image.size;
    
//    UIImage *newImage;
    //按照宽度缩放
    CGFloat zoom = size.width / toSize.width;
    
    if (size.height > toSize.height * zoom ) {//高度大于要显示的区域
        return CGRectMake(0, 0, size.width, toSize.height * zoom);
    }else if (size.height / zoom <= toSize.height){//高度小于要显示区域，,太长截取
        UIGraphicsBeginImageContext(size);
        [image drawInRect:CGRectMake(0, 0, size.width,size.height)];
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        image = scaledImage;
        
        return CGRectMake(0, 0, size.width, size.height);
    }else{
        return CGRectMake(0, 0, size.width, size.height);
    }
    
}

- (UIViewController *)findViewController
{
    id target=self;
    while (target) {
        target = ((UIResponder *)target).nextResponder;
        if ([target isKindOfClass:[UIViewController class]]) {
            break;
        }
    }
    return target;
}

- (void)setBackgroundColors:(NSArray *)colorArray gradientType:(GradientColorType)gradientColorType
{
    [self setBackgroundColors:colorArray cornerRadius:0 gradientType:gradientColorType];
}

- (void)setBackgroundColors:(NSArray *)colorArray cornerRadius:(CGFloat)cornerRadius gradientType:(GradientColorType)gradientColorType
{
    [self setBackgroundColors:colorArray cornerRadius:cornerRadius borderWidth:0 borderColor:[UIColor clearColor] gradientType:gradientColorType];
}

- (void)setBackgroundColors:(NSArray *)colorArray cornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor gradientType:(GradientColorType)gradientColorType
{
    [self.superview layoutIfNeeded];
    CAGradientLayer *gradient = [CAGradientLayer layer];
    //设置开始和结束位置(设置渐变的方向)
    switch (gradientColorType) {
        case GradientColorTypeTopToBottom:
            gradient.startPoint = CGPointMake(0, 0);
            gradient.endPoint = CGPointMake(0, 1);
            break;
        case GradientColorTypeLeftToRight:
            gradient.startPoint = CGPointMake(0, 0);
            gradient.endPoint = CGPointMake(1, 0);
            break;
        case GradientColorTypeUpleftTolowRight:
            gradient.startPoint = CGPointMake(0, 0);
            gradient.endPoint = CGPointMake(1, 1);
            break;
        case GradientColorTypeUprightTolowLeft:
            gradient.startPoint = CGPointMake(1, 0);
            gradient.endPoint = CGPointMake(0, 1);
            break;
        default:
            break;
    }
    
    gradient.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
    NSMutableArray *cgColors = [NSMutableArray arrayWithCapacity:0];
    for (UIColor *color in colorArray) {
        [cgColors addObject:(id)color.CGColor];
    }
    
    
    gradient.colors = cgColors;
    gradient.cornerRadius = cornerRadius;
    gradient.borderWidth = borderWidth;
    gradient.borderColor = borderColor.CGColor;
    [self.layer insertSublayer:gradient atIndex:0];
}

- (void)setRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
{
    CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = rect;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

/**
 *  在view上绘制虚线
 *
 *  @param lineView    需要绘制成虚线的view
 *  @param lineLength  虚线的宽度
 *  @param lineSpacing 虚线的间距
 *  @param lineColor   虚线的颜色
 */
+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor {
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

@end
